/**
 *
 * @author
 *
 */
class SoundManager {
    public constructor() {

    }

    private static instance: SoundManager;
    public static getInstance(): SoundManager {
        if (this.instance == null) {
            this.instance = new SoundManager();
        }
        return this.instance;
    }
    /**声音 */
    private sounds = {};
    /**音量 */
    public _setSoundVolume: number = 1;
    /**背景声音 */
    public bgSound: egret.Sound;
    public effectSound: egret.Sound;
    /**背景声道 */
    private bgChannel: egret.SoundChannel;
    /**所有音效声道 */
    private effChannel: egret.SoundChannel;
    /**按键音效声道 */
    private btnChannel: egret.SoundChannel;
    private bg_position: number = 0;
    public isok: boolean = true;
    public sound_switch: boolean = false;
    public sound_effect_switch: boolean = true;
    public scene: number = 0;
    private completeFun: Function = null;
    private num = 0;
    private loadnum() {
        this.num++;
        if (this.num == 12) {
            if (this.completeFun != null) {
                this.completeFun();
            }
        }
    }
    /**注册声音 */
    public rigisterSound(soundName: string) {
        let sound = RES.getRes(soundName);
        this.loadnum();
        this.sounds[soundName] = sound;
    }
    /**加载声音 */
    public soundLoad(fun?: Function) {
        this.rigisterSound("firstblood_mp3");
        this.rigisterSound("doublekill_mp3");
        this.rigisterSound("triplekill_mp3");
        this.rigisterSound("quatarykill_mp3");
        this.rigisterSound("pentakill_mp3");
        this.rigisterSound("click_mp3");
        this.rigisterSound("shutdown_mp3");
        this.rigisterSound("gameOver_mp3");
        this.rigisterSound("killingspree_mp3");
        this.rigisterSound("monsterkill_mp3");
        this.rigisterSound("godlike_mp3");
        this.rigisterSound("legendary_mp3");
        if (fun != undefined) {
            fun();
        }
    }
    /**打开/关闭背景音乐*/
    public playBgSound(b: boolean, str: string): void {

        this.sound_switch = b;
        if (b) {
            this.bgSound = RES.getRes(str);
            if (this.bgChannel != null) {
                this.bgChannel.stop();
                this.bgChannel = null;
            }
            this.bgChannel = this.bgSound.play(this.bg_position, 0);
        }
        else {
            if (this.bgChannel != null) {
                this.bg_position = this.bgChannel.position;
                this.bgChannel.stop();
                this.bgChannel = null;
            }
            else {
                this.bg_position = 0;
            }
        }
    }

   // private isPlayEffectEnd = false;

    /**播放音效*/
    public playEffectSound(str: string): void {
        // if (this.isPlayEffectEnd == true) {
        //     console.log("播放未結束"+str);
        //     return;
        // }
        let bgS: egret.Sound = this.sounds[str];
        if (this.effChannel != null) {
            this.effChannel.stop();
        }
        if (bgS != null) {
           // this.isPlayEffectEnd = true;
            this.effChannel = bgS.play(0, 1);
            this.effChannel.volume = this._setSoundVolume;
           // this.effChannel.addEventListener(egret.Event.SOUND_COMPLETE, this.onPlayEffectEnd, this);
        }
        console.log(this.sounds[str] + "击杀");
    }

    // private onPlayEffectEnd() {
    //     this.isPlayEffectEnd = false;

    // }
    /**播放按钮音效 */
    public playButtonSound(soundName: string) {
        let btns: egret.Sound = this.sounds[soundName];
        if (this.btnChannel != null) {
            this.btnChannel.stop();
        }
        if (btns != null) {
            this.btnChannel = btns.play(0, 1);
            this.btnChannel.volume = this._setSoundVolume;
        }
    }
}
